package name.williamwall.buxoid;

/**
 * This software is made available under the Gnu General Public License v3
 * and has absolutely no waranty. You may change the software to suit your needs.
 * 
 * Author: William Wall, IV
 */

import java.text.NumberFormat;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class AnalysisAdapter extends SimpleCursorAdapter {
	private static NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
	
	public AnalysisAdapter(Context context, int layout, Cursor c, String[] from, int [] to) {
		super(context, layout, c, from, to);
		moneyFormat.setCurrency(Buxoid.mDb.getCurrency());
	}

	@Override
	public void setViewImage(ImageView v, String value) {
		int id = v.getId();
		
		if (id == R.id.row_analysis_color && value != null) {
			int color = Integer.parseInt(value, 16);
			int[] colors = new int [256];
			for (int i = 0; i < 256; i++) {
				colors[i] = color;
			}
			Bitmap b = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.RGB_565);
			v.setImageBitmap(b);
		} else {
			super.setViewImage(v, value);
		}
	}

	@Override
	public void setViewText(TextView v, String text) {
		int id = v.getId();
		if (id == R.id.row_analysis_amount) {
			v.setText(moneyFormat.format(Double.parseDouble(text)));
		} else {
			super.setViewText(v, text);
		}
	}
}
